package com.campus.service.impl;

import com.campus.constant.MessageConstant;
import com.campus.constant.StatusConstant;
import com.campus.context.CurrentHolder;
import com.campus.exception.*;
import com.campus.mapper.StudyRoomMapper;
import com.campus.pojo.dto.StudyRoomDTO;
import com.campus.pojo.dto.StudyRoomPageQueryDTO;
import com.campus.pojo.entity.StudyRoom;
import com.campus.pojo.vo.StudyRoomVO;
import com.campus.result.PageResult;
import com.campus.service.StudyRoomService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudyRoomServiceImpl implements StudyRoomService {
    @Autowired
    private StudyRoomMapper studyRoomMapper;

    @Override
    public void save(StudyRoomDTO studyRoomDTO) {
        // 1.查询数据库中是否有相同名称的
        String name = studyRoomDTO.getName();
        StudyRoom studyRoom = studyRoomMapper.getByName(name);
        if (studyRoom != null) {
            // 存在相同名称的
            throw new StudyRoomNameDuplicateException(MessageConstant.STUDYROOM_NAME_ALREADY_EXISTS);
        }

        // 2.构建entity对象-library
        studyRoom = StudyRoom.builder()
                .location(studyRoomDTO.getLocation())
                .name(studyRoomDTO.getName())
                .status(StatusConstant.STATUS_NORMAL)
                .build();


        //3.调用mapper方法
        studyRoomMapper.insert(studyRoom);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //1.是否处于被预约状态
        for (Long id : ids) {
            StudyRoom studyRoom = studyRoomMapper.getById(id);
            if (studyRoom.getStatus() == StatusConstant.STATUS_RESERVED) {
                throw new DeletionNotAllowedException(MessageConstant.STUDYROOM_IS_RESERVED);
            }
        }

        //2.调用mapper方法
        studyRoomMapper.deleteByIds(ids);

    }

    @Override
    public PageResult<StudyRoomVO> pageQuery(StudyRoomPageQueryDTO studyRoomPageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(studyRoomPageQueryDTO.getCurrentPage(), studyRoomPageQueryDTO.getPageSize());

        //2.调用mapper层
        Page<StudyRoomVO> page = studyRoomMapper.pageQuery(studyRoomPageQueryDTO);

        //3.返回结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //1.查询自习室是否存在
        StudyRoom studyRoom = studyRoomMapper.getById(id);
        if (studyRoom == null) {
            //不存在
            throw new ResourceNotFoundException(MessageConstant.STUDYROOM_NOT_FOUND);
        }

        //2.构建entity对象-StudyRoom
        //如果要改为正常状态即无预约 此时需将userId置为null
        if (status == StatusConstant.STATUS_NORMAL) {
            studyRoom = StudyRoom.builder()
                    .id(id)
                    .status(status)
                    .userId(null)  // 置空userId表示无人预约
                    .build();
        } else {
            studyRoom = StudyRoom.builder()
                    .id(id)
                    .status(status)
                    .userId(CurrentHolder.getCurrentId())
                    .build();
        }


        //3.调用mapper方法
        studyRoomMapper.update(studyRoom);

    }

    @Override
    public StudyRoom getById(Long id) {
        return studyRoomMapper.getById(id);
    }

    @Override
    public void update(StudyRoomDTO studyRoomDTO) {

        //1.构建entity对象-StudyRoom 以便update方法复用
        StudyRoom studyRoom = StudyRoom.builder()
                .id(studyRoomDTO.getId())
                .name(studyRoomDTO.getName())
                .location(studyRoomDTO.getLocation())
                .build();

        //2.调用mapper方法
        studyRoomMapper.update(studyRoom);
    }
}
